<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 组织管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="success" class="handle-del mr10" @click="addEdit">新增</el-button>
                <!--<el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>-->
                <el-input v-model="query.name" clearable placeholder="组织名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="orgId" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="orgName" label="组织名称"></el-table-column>
                <el-table-column prop="orgType" label="组织类型"></el-table-column>
                <el-table-column prop="description" label="组织描述"></el-table-column>

                <!--<el-table-column prop="createTime" label="创建时间"></el-table-column>-->
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="titleType" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="组织名称">
                    <el-input v-model="form.orgName"></el-input>
                </el-form-item>
                <el-form-item label="组织描述">
                    <el-input v-model="form.description"></el-input>
                </el-form-item>
                <el-form-item label="组织类型" v-if = "this.operFlag=='0'">
                    <el-radio-group v-model="form.orgType">
                        <el-radio label="0">团学组织</el-radio>
                        <el-radio label="99">班级</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            operFlag:'9',//操作标识：9-初始化 0-新增 1-编辑
            titleType:'' ,//此处初始化变量
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        async getData(){
	        let res = await this.$Http.getOrganizationList(
	        	{
	        		pageNum:this.query.pageIndex,
	        	},
                true
	        );
	        this.tableData = res.data.list;
	        this.pageTotal = res.data.total || 10;
	    },
        
        
//      getData() {
//          fetchData(this.query).then(res => {
//              this.tableData = res.list;
//              this.pageTotal = res.pageTotal || 50;
//          });
//
//      },
        // 触发搜索按钮
        async handleSearch() {
            let res = await this.$Http.getOrganizationByParams(
	        	{
	        		
	        		orgName : this.query.name
	        		
	        	},
                true
	        );
	        //console.log(res.data);
	        //console.log(res.data.total);
	        this.tableData = res.data.list;
	        this.pageTotal = res.data.total || 10;
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.delOperate(row.orgId);
                })
                .catch(() => {});
        },
        //执行删除
        async delOperate(value){
            let res = await this.$Http.deleteOrg(
	        	{
	        		organizationId : value
	        	},
	            true
	        );
            console.log(res);
	        if (res.status == 0){
	        	//this.$message.success(res.msg);
	        	this.delList = [];
	        }
            this.$message.success(res.msg);
            this.getData();
            this.multipleSelection = [];
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.operFlag='1';
            this.titleType ="编辑";
            this.editVisible = true;
        },
        // 新增操作
        addEdit(){
        	this.form = [];
        	this.operFlag='0';
        	this.titleType ="新增";
            this.editVisible = true;
        },
        // 保存编辑
        async saveEdit() {
            
            let org = this.form;
        	//检查必输字段
        	if(org.orgName=='' || org.orgName==undefined){
        		this.$message.error("组织名称不能为空!");
        		return;
        	}
        	if(org.description=='' || org.description==undefined){
        		this.$message.error("组织描述不能为空!");
        		return;
        	}
        	if(org.orgType=='' || org.orgType==undefined){
        		this.$message.error("组织类型不能为空!");
        		return;
        	}
        	this.editVisible = false;
        	/*let formReq = new FormData();
        	formReq.append("orgName",org.orgName);
        	formReq.append("description",org.description);
        	if (org.orgType == '99'){
        		formReq.append("orgType",org.orgType);
        	}*/
        	let res ;
            //新增
            if (this.operFlag=='0'){
            	if (org.orgType == '99'){
            		res = await this.$Http.addOrg(
			        	{
			        		orgName:org.orgName,
			        		description:org.description,
			        		orgType:org.orgType
			        	},
		                true
		        	);
            	}else{
            		res = await this.$Http.addOrg(
			        	{
			        		orgName:org.orgName,
			        		description:org.description
			        	},
		                true
		        	);
            	}
		        
		        console.log(res);
		        if(res.status==0){
		        	this.$message.success(res.msg);
		        	this.getData();
		        }else{
		        	this.$message.error(res.msg);
		        }
	            this.getData();
            }
            
            //修改
            if(this.operFlag=='1'){
            	
            	let res = await this.$Http.editOrg(
		        	{
		        		orgId : org.orgId,
		        		orgName:org.orgName,
			        	description:org.description,
			        	orgType:org.orgType
		        	},
	                true
		        );
		        console.log(res);
		        if(res.status==0){
		        	this.$message.success(res.msg);
		        	this.getData();
		        }else{
		        	this.$message.error(res.msg);
		        }
	            this.getData();
            }
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
